package com.timothy.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.timothy.android.util.XMLUtil;
import com.timothy.answer.ChoiceResult;
import com.timothy.question.BaseQuestion;
import com.timothy.statistics.ButtonStatistics;
import com.timothy.statistics.SlideStatistics;

public class StatisticsBuilder {
	
  /**
   * <Message Type="SlideResult">
   *    <Content>
   *	      <Question CorrectChoice="n" QuestionName="What is your phone size?" SlideIndex="3" DisplayStyle="1">
   *	        <Choice Index="1" Chosen="1"/>
   *	        <Choice Index="2" Chosen="2"/>
   *	        .........
   *	        <Choice Index="n" Chosen="x"/>
   *	      </Question>
   *	 </Content>
   * </Message>
   * @param content
   * @return
   */
	public static ButtonStatistics parseSTTwoButtonAlphaAnswer(String content){
		ButtonStatistics statistics = new ButtonStatistics();
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		BaseQuestion bq = new BaseQuestion();
		bq.setQuestionName( questionName );
		statistics.setQuestion( bq );
		List<ChoiceResult> crList = new ArrayList<ChoiceResult>();
		List<String> cs = XMLUtil.getChildren( content );
		int totalCount=0;
		int index = 0;
		for( String c: cs ) {
			String mark = XMLUtil.getPropertyValue( c , "Index");
			String count = XMLUtil.getPropertyValue( c , "Chosen");
			String text = XMLUtil.getPropertyValue( c , "Text");
			
			ChoiceResult cr = new ChoiceResult();
			cr.setIndex(index++);
			cr.setMark( mark );
			cr.setCount( Integer.valueOf(count));
			cr.setContent(text);
			
			crList.add( cr );
			totalCount += cr.getCount();
		}
		statistics.setChoiceResults( crList );
		statistics.setTotal(totalCount);
		return statistics;
	}
	
	/**
	 *	<Message Type="SlideResult">
	 *	    <Content>
	 *	      <Question QuestionName="How old are you?" SlideIndex="5" DisplayStyle="10">
	 *	          <Total>1000</Total>
	 *	          <Average>100</Average>
	 *	      </Question>      
	 *	    </Content>
	 *	</Message> 
	 */
	public static SlideStatistics parseSlideStatistics(String content){
		SlideStatistics statistics = new SlideStatistics();
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		BaseQuestion bq = new BaseQuestion();
		bq.setQuestionName( questionName );
		statistics.setQuestion( bq );
		
		List<String> cs = XMLUtil.getChildren( content );
		String total = XMLUtil.getElementValue( cs.get(0));
		String average = XMLUtil.getElementValue(cs.get(1));
		
		statistics.setTotal(Integer.valueOf(total));
		statistics.setAverage(Integer.valueOf(average));
		return statistics;
	}

	/**
	 *	<Message Type="SlideResult">
	 *	    <Content>
	 *	      <Question QuestionName="How old are you?" SlideIndex="5" DisplayStyle="10">
	 *	          <answer>144</answer>
	 *	      </Question>      
	 *	    </Content>
	 *	</Message> 
	 */
	public static FreeFormStatistics parseFreeFormStatistics(String content){
		FreeFormStatistics statistics = new FreeFormStatistics();
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		BaseQuestion bq = new BaseQuestion();
		bq.setQuestionName( questionName );
		statistics.setQuestion( bq );
		
		List<String> cs = XMLUtil.getChildren( content );
		String answer = XMLUtil.getElementValue( cs.get(0));
		
		statistics.setAnswer(answer);
		return statistics;
	}
	  /**
	   * <Message Type="SlideResult">
	   *    <Content>
	   *	      <Question Priority="1" QuestionName="What is your phone size?" SlideIndex="3" DisplayStyle="1">
	   *	        <Choice Index="1" Chosen="1"/>
	   *	        <Choice Index="2" Chosen="2"/>
	   *	        .........
	   *	        <Choice Index="n" Chosen="x"/>
	   *	      </Question>
	   *	 </Content>
	   * </Message>
	   * @param content
	   * @return
	   */
	public static PriorityStatistics parsePriorityStatistics(String content){
		PriorityStatistics statistics = new PriorityStatistics();
		
		Map<Integer,List<ChoiceResult>> priorityMap = new HashMap<Integer,List<ChoiceResult>>();
		
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		BaseQuestion bq = new BaseQuestion();
		bq.setQuestionName( questionName );
		statistics.setQuestion( bq );
		
		List<String> priorityList = XMLUtil.getChildren( content );
		
		for( String priority: priorityList ) {
			String level = XMLUtil.getPropertyValue( priority , "level");
			List<String> choiceList = XMLUtil.getChildren( priority );
			List<ChoiceResult> crList = new ArrayList<ChoiceResult>();
			int totalCount=0;
//			int index = 0;
			for( String choice: choiceList ) {
				String index = XMLUtil.getPropertyValue( choice , "Index");
				String text = XMLUtil.getPropertyValue( choice , "Text");
				String count = XMLUtil.getPropertyValue( choice , "Chosen");
				ChoiceResult cr = new ChoiceResult();
				cr.setIndex(Integer.valueOf(index));
				cr.setMark( text );
				cr.setContent(text);
				cr.setCount( Integer.valueOf(count));
				crList.add( cr );
				totalCount += cr.getCount();
			}
			
			for(ChoiceResult cr: crList ) {
				cr.setShare(1.0*cr.getCount()/totalCount);
			}
			
			priorityMap.put(Integer.valueOf(level), crList);
			
		}
		
		statistics.setPriorityMap(priorityMap);
		
		return statistics;
	}
	
	/**
	 * 
	 * @param content
	 * @return
	 */
	public static GradingStatistics parseGradingStatistics(String content){
		GradingStatistics statistics = new GradingStatistics();
		String questionName = XMLUtil.getPropertyValue( content , "QuestionName");
		BaseQuestion bq = new BaseQuestion();
		bq.setQuestionName( questionName );
		statistics.setQuestion( bq );
		List<ChoiceResult> crList = new ArrayList<ChoiceResult>();
		List<String> cs = XMLUtil.getChildren( content );
		int totalCount=0;
//		int index = 0;
		for( String c: cs ) {
			String index = XMLUtil.getPropertyValue( c , "Index");
			String total = XMLUtil.getPropertyValue( c , "Total");
			String text = XMLUtil.getPropertyValue( c , "Text");
			
			ChoiceResult cr = new ChoiceResult();
			cr.setIndex(Integer.valueOf(index));
			cr.setCount( Integer.valueOf(total));
			cr.setContent(text);
			
			crList.add( cr );
			totalCount += cr.getCount();
		}
		statistics.setChoiceResults( crList );
		statistics.setTotal(totalCount);
		return statistics;
	}
}
